env.GIT_URL = 'git项目地址'

env.CREDENTIALS_ID = '你的git凭据，在凭据中查看唯一标识'

env.REMOTE_IP = '需要部署的远程服务ip'

env.REMOTE_DIR = 'jar包所在远程服务器目录'

env.TARGET_PREFIX = 'xxx/target'   //jenkins打包的jar所在的target

env.PL = 'xx-portal/xx-portal-starter'  //{groupId}:{artifactId}或者所选模块的相对路径(多个模块以逗号分隔)


pipeline {
    agent any
    stages {

        stage('拉取代码') {
            steps {
                checkout scmGit(branches: [[name: "${GIT_BRANCH}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${CREDENTIALS_ID}", url: "${GIT_URL}"]])
            }
        }

        stage('打jar包') {
            steps {
                sh '''
                mvn install -pl ${PL} -am -Dmaven.test.skip=true -T4
                '''
            }
        }

        stage('发送服务器启动服务') {
            steps {       
				// sh deploy.sh restart 这个deploy.sh是在远程服务器目录下面，启动java应用，可以根据自己的需要修改
                sshPublisher(publishers: [sshPublisherDesc(configName: "${REMOTE_IP}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "cd ${REMOTE_DIR} && sh deploy.sh restart", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "${REMOTE_DIR}", remoteDirectorySDF: false, removePrefix: "${TARGET_PREFIX}", sourceFiles: "${TARGET_PREFIX}/*.jar")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }

		// 下面一段可以不要，我的测试Jenkins服务器磁盘不足，就使用下面命令每次删除打好的包
        stage('删除jar包') {
            steps {				
                sh ''' 
                
                #find ./ -name '*SNAPSHOT.jar' -type f -print | xargs rm -rf ;
                
                '''
            }
        }

    }
}